import api from '@api'

const cateType = {
  yzyg: '1,8,',
  kzgh: '1,3,',
  zxsg: '1,2',
  yzyh: '5,6',
  jphs: '5,7,',
  mxd: '10,11,',
  ogxl: '10,12,'
}

const state= {
  category: {
    yzyg: {
      list: [],
      pageNum: 0,
      page: 1
    },
    zxsg: {
      list: [],
      pageNum: 0,
      page: 1
    },
    kzgh: {
      list: [],
      pageNum: 0,
      page: 1
    },
    mxd: {
      list: [],
      pageNum: 0,
      page: 1
    },
    ogxl: {
      list: [],
      pageNum: 0,
      page: 1
    },
    zygh: {
      list: [],
      pageNum: 0,
      page: 1
    },
    jphs: {
      list: [],
      pageNum: 0,
      page: 1
    },
    yzyh: {
      list: [],
      pageNum: 0,
      page: 1
    }
  }
}

const getters= {
  ['category'] (state) {
    return state.category;
  }
}

const actions = {
  async ['getList'] ({commit}, {cate, type, page}) {
    let config = {
      goIsSale: 1,
      goodsType: cateType[cate],
      searchKey: '',
      CurrentPage: page,
      PageSize: 5,
      orderBy: 8
    }
    let {data: {goodsList, pageNum, StatusCode}} = await api.get('api/v1/Goods/goodsList.ashx', config);
    if(StatusCode === '200') {
      commit('setList', {cate,list:goodsList, pageNum: Number(pageNum),page})
    }
  },
  ['addList'] ({commit, dispatch, state}, {cate, type }) {
    let page = state.category[cate].page +1
    if(page <= state.category[cate].pageNum) {
      let config = {
        goIsSale: 1,
        goodsType: cateType[cate],
        searchKey: '',
        CurrentPage: page,
        PageSize: 5,
        orderBy: 8
      }
      return api.get('api/v1/Goods/goodsList.ashx', config)
        .then(function ({data: {goodsList, pageNum, StatusCode}}) {
          if(StatusCode === '200') {
            commit('addList', {cate ,list:goodsList, pageNum: Number(pageNum),page})
          }
        })


    }
  }
}
const mutations = {
  ['setList'] (state, {cate, list, pageNum, page}) {
    state.category[cate] = {
      list: list, pageNum, page
    }
  },
  ['addList'] (state, {cate, list, pageNum, page}) {
    let goodsList = state.category[cate].list.concat(list);
    state.category[cate] = {
      list: goodsList, pageNum, page
    }
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}